package com.yksj.healthtalk.ui.interestwall;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.yksj.healthtalk.comm.RootFragment;
import com.yksj.healthtalk.entity.TagEntity;
import com.yksj.healthtalk.ui.R;

/**
 * 兴趣墙 主fragment
 * 
 * @author jack_tang
 */
public class InterestMainFragment extends RootFragment implements
		OnClickListener, OnCheckedChangeListener, OnPageChangeListener {
	public static final int REQUEST_CODE = 0x0000001;
	// 操作瀑布流fragment
	private ViewPager interestPager;// 下半部分 viewpager
	private InterestPagerAdapter adapter;
	/**
	 * radiogroup 导航栏
	 */ 
	private RadioGroup mRadioGroup;
	/**
	 * 默认第一个被选中
	 */
	private RadioButton mRadioButton;
	/**
	 * 选中查看某分类中的二级分类图片fragment
	 */
//	private TextView centerText;
	/**
	 * 大类id
	 */
	private String classId = "0";
	/**
	 * 小类id
	 */
	private String secondaryId = "0";
	
	private ViewGroup fragmentcontent;
	/**
	 * 一级分类fragment
	 */
	private StairCategoryChooseFragment fragment;
	/**
	 * 存储当前的position
	 */
	private int currentPosition = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initData();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.interest_main, container, false);
		initView(view);
		return view;
	}

	public void initData() {
		adapter = new InterestPagerAdapter(this.getChildFragmentManager(),
				getActivity(), classId,secondaryId);
	}

	public void initView(final View view) {
		initTitle(view);
		// radiogroup init
		mRadioButton = (RadioButton) view.findViewById(R.id.radio_hot);
		mRadioButton.setChecked(true);//默认选中的一个
		mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_select);
		mRadioGroup.setOnCheckedChangeListener(this);
		for (int i = 0; i < mRadioGroup.getChildCount(); i++)
			mRadioGroup.getChildAt(i).setOnClickListener(this);//给每个radiogroup内的控件设置点击事件
		// view pager init
		interestPager = (ViewPager) view.findViewById(R.id.interest_pager);
		interestPager.setAdapter(adapter);
		interestPager.setOnPageChangeListener(this);
		fragmentcontent = (ViewGroup) view.findViewById(R.id.fragmentcontent);
		
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}
	

	/**
	 * 初始化title
	 * @param view
	 */
	public void initTitle(View view) {
		// title init;
		super.initTitleView(view);
		
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("分享见闻");
		
		titleRightBtn2.setBackgroundResource(R.drawable.icon_release_press);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		
		titleRightBtn.setVisibility(View.VISIBLE);
		titleRightBtn.setBackgroundResource(R.drawable.icon_search_press);
		titleRightBtn.setOnClickListener(this);
	}
	
	/**
	 * 搜索界面
	 */
	public void startCategoryFragment() {
		//点击分类选择时弹出第一个分类选择
		fragment = StairCategoryChooseFragment.getInstance();
		//设置监听器，点击一级分类调用此监听器
//		fragment.setCategoryChooseListener(this);
		//点击摄影馆调用此监听器
//		fragment.setOnPhotoGalleryButtonClick(this);
		
		FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
		transaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
		transaction.add(R.id.fragmentcontent, fragment);
		fragmentcontent.setClickable(true);
		transaction.commit();
	}
	
	@Override
	public void onClick(View v) {
		Intent intent = null;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed(v);
			break;
		case R.id.title_right2://发布
			intent = new Intent(this.getActivity(), InterestReleasePic.class);
			startActivity(intent);
			break;
		case R.id.title_right://搜索功能
			intent=new Intent(this.getActivity(), InsterswallTypeChooseActivity.class);
			startActivityForResult(intent, 4000);
			break;
		case R.id.radio_new:
			interestPager.setCurrentItem(2, true);
			break;
		case R.id.radio_all:
			interestPager.setCurrentItem(3, true);
			break;
		case R.id.radio_recommend:
			interestPager.setCurrentItem(1, true);
			break;
		case R.id.radio_hot:
			interestPager.setCurrentItem(0, true);
			break;
		case R.id.radio_mine:
			interestPager.setCurrentItem(4, true);
			break;
		}
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		currentPosition = arg0;
		switch (arg0) {
		case 0:
			((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
			break;
		case 1:
			((RadioButton) mRadioGroup.getChildAt(1)).setChecked(true);
			break;
		case 2:
			((RadioButton) mRadioGroup.getChildAt(2)).setChecked(true);
			break;
		case 3:
			((RadioButton) mRadioGroup.getChildAt(3)).setChecked(true);
			break;
		case 4:
			((RadioButton) mRadioGroup.getChildAt(4)).setChecked(true);
			break;
		}
	}
	
	boolean flag = false;
	
	public class InterestPagerAdapter extends FragmentStatePagerAdapter {
		//一级分类id
		private String classId;
		//二级分类id
		private String secondaryId;
		//所有fragment的集合
		private final List<WaterFallFragment> fragments;
		//我发布和我收藏fragment
		private InterestUserRelease interestUserReleaseFragment;
		
		private String type = "";

		public InterestPagerAdapter(FragmentManager fm, Context context,
				String classId,String secondaryId) {
			super(fm);
			this.classId = classId;
			this.secondaryId = secondaryId;
			fragments = new ArrayList<WaterFallFragment>();
			initFragments();
		}

		@Override
		public Fragment getItem(int arg0) {
			if (arg0 == 4) {
				interestUserReleaseFragment = InterestUserRelease
						.newInstance(4);
				return interestUserReleaseFragment;
			} else if(arg0 == 3){
				Fragment fragment = fragments.get(arg0);
				if(!flag){
					fragment.getArguments().remove("entity");
				}else{
					flag = false;
				}
				return fragment;
			}else
			return fragments.get(arg0);
		}

		 @Override
		 public int getItemPosition(Object object) {
		 return POSITION_NONE;
		 }

		public void initFragments() {
			//初始化四个fragment
			
			for (int i = 0; i < 4; i++) {
				WaterFallFragment mFallFragment = WaterFallFragment.newInstance(i, 1, classId,secondaryId);
				fragments.add(mFallFragment);
			}

		}
		
		public void refreshUserRelease(int position) {
			if(interestUserReleaseFragment!=null)
			interestUserReleaseFragment.refreshData(position);
		}
		
		@Override
		public int getCount() {
			return fragments.size()+1;
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(4000==requestCode){
			if(resultCode == Activity.RESULT_OK){
				TagEntity entity =(TagEntity)data.getParcelableExtra("entity");
				WaterFallFragment fragment = (WaterFallFragment)adapter.getItem(3);
				if(interestPager.getCurrentItem()!=3){
					Bundle arguments = fragment.getArguments();
					arguments.putParcelable("entity", entity);
					flag = true;
					interestPager.setCurrentItem(3, true);
				}else{
					fragment.onDataChangeByType(entity);
				}
			}
		}else{
			if(resultCode!=Activity.RESULT_OK) return;
			int picid = data.getIntExtra("picid",-1);
			if(picid!=-1) adapter.refreshUserRelease(picid);	
		}
	}
	
	public void onChangeNameList() {
		interestPager.setCurrentItem(3,true);
		WaterFallFragment fragment = (WaterFallFragment)adapter.getItem(3);
		TagEntity entity = new TagEntity("Name", getActivity().getIntent().getStringExtra("NAME"));
		fragment.getArguments().putParcelable("entity", entity);
		flag = true;
	}
	
}
